package Gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import client.ConnectionClient;
import client.MakeDigest;

/**
 * The GUI pane where a new user logs in. Contains a text field where the user
 * id is entered, a password field where the password is entered and a button to
 * log in.
 */
public class UserLoginPane extends BasicPane {
	private static final long serialVersionUID = 1;
	/**
	 * The text fields where the user id and password is entered.
	 */
	private JTextField[] fields;

	/**
	 * The number of the field where the user id is entered.
	 */
	private static final int USER_ID = 0;

	/**
	 * The number of the field where the user password is entered.
	 */
	private static final int PASSWD_ID = 1;

	/**
	 * The total number of fields in the fields array.
	 */
	private static final int NBR_FIELDS = 2;

	/**
	 * The hash function which will digest the password.
	 */
	private final MakeDigest md;

	/**
	 * The connection client which will communicate with the server.
	 */
	private final ConnectionClient cc;

	/**
	 * Create the login pane.
	 * 
	 * @param cc
	 *            The connection client object.
	 */
	public UserLoginPane(ConnectionClient cc) {
		super(cc);
		this.cc = cc;
		md = new MakeDigest();
	}

	/**
	 * Create the top panel, consisting of the text fields.
	 * 
	 * @return The top panel.
	 */
	@Override
	public JComponent createTopPanel() {
		String[] texts = new String[NBR_FIELDS];
		texts[USER_ID] = "Enter user id (XXXXXX-XXXX): ";
		texts[PASSWD_ID] = "Enter password: ";
		fields = new JTextField[NBR_FIELDS];
		fields[USER_ID] = new JTextField(20);
		fields[PASSWD_ID] = new JPasswordField(20);
		return new InputPanel(texts, fields);
	}

	/**
	 * Create the bottom panel, consisting of the login button and the message
	 * line.
	 * 
	 * @return The bottom panel.
	 */
	@Override
	public JComponent createBottomPanel() {
		JButton[] buttons = new JButton[1];
		buttons[0] = new JButton("Login");
		ActionHandler actHand = new ActionHandler();
		fields[USER_ID].addActionListener(actHand);
		return new ButtonAndMessagePanel(buttons, messageLabel, actHand);
	}

	/**
	 * Perform the entry actions of this pane, i.e. clear the message line.
	 */
	@Override
	public void entryActions() {
		clearMessage();
	}

	/**
	 * A class which listens for button clicks.
	 */
	class ActionHandler implements ActionListener {
		/**
		 * Called when the user clicks the login button. Checks with the server
		 * if the user exists, and if so notifies the CurrentUser object.
		 * 
		 * @param e
		 *            The event object (not used).
		 */
		public void actionPerformed(ActionEvent e) {
			String userName = fields[USER_ID].getText();
			String userHash = md
					.makeHash(fields[PASSWD_ID].getText(), userName);
			fields[PASSWD_ID].setText("");
			CurrentUser.instance().loginAs(userName, userHash);
			System.out.println("Försöker logga in");
			System.out.println(CurrentUser.instance().getCurrentUserId() + "  "
					+ CurrentUser.instance().getCurrentUserHash());

			String compHash = cc.sendData(userName + ";" + userHash
					+ ";gethash;padding");

			System.out.println("Inloggad");
			System.out.println("Comphash: " + compHash);

			if (compHash != null) {
				if (compHash.equals("ACK")) {
					displayMessage("Logged in as "
							+ CurrentUser.instance().getCurrentUserId());
				} else {
					CurrentUser.instance().loginAs(null, null);
					displayMessage("Unable to log in as user " + userName);
				}
			} else {
				displayMessage("User " + userName + " doesn't exists!");
				CurrentUser.instance().loginAs(null, null);
			}
		}
	}
}
